//
//  XYJLXResultViewController.m
//  徙游记-01
//
//  Created by hcios on 2017/7/12.
//  Copyright © 2017年 鹿林. All rights reserved.
//

#import "XYJLXResultViewController.h"
#import "SeverceHttpRequest.h"
#import "XYJLXResultModel.h"
#import "XYJLXResultFrameModel.h"
#import "XYJLXResultTableViewCell.h"
#import "UIImageView+WebCache.h"

@interface XYJLXResultViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource>{
    
    NSMutableArray * _dataSectionArr;
    NSMutableArray * _dataImageUrlSectionArr;
    UITableView * _tableView;
    UICollectionView * cellCollectionView;
    NSIndexPath * collectionViewSection;
    NSIndexPath * indexPathSection;
}

@end

@implementation XYJLXResultViewController


- (instancetype)init
{
    self = [super init];
    if (self) {
        _dataSectionArr = [[NSMutableArray alloc] init];
        _dataImageUrlSectionArr = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self netWorking];
    [self initTable];
    // Do any additional setup after loading the view.
}

- (void)initTable {
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSInteger count = [_dataSectionArr[section] count];
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString * cellID = [NSString stringWithFormat:@"cellID%ld%ld",(long)[indexPath section],(long)[indexPath row]];
    XYJLXResultTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[XYJLXResultTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    indexPathSection = indexPath;
    cell.frameModel = _dataSectionArr[indexPath.section][indexPath.row];
    [self layoutCollectionViewWith:cell.frameModel.scrollFrame andIndexRow:indexPath];
    [cell addSubview:cellCollectionView];
    return cell;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if ([scrollView isKindOfClass:[UICollectionView class]]) {
        CGPoint point = scrollView.center;
        point = [_tableView convertPoint:point fromView:scrollView.superview];
        NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:point];
        indexPathSection = indexPath;
    }
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return _dataSectionArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    XYJLXResultFrameModel * frameModel = _dataSectionArr[indexPath.section][indexPath.row];
    return frameModel.cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}


- (void)netWorking {
    
    NSString * urlString = [NSString stringWithFormat:@"http://chanyouji.com/api/attractions/%@.json",_selfID];
    [[SeverceHttpRequest sharedManager] GET:urlString parameters:nil success:^(id reponseObject) {
        //第一层遍历
        for (NSDictionary * dict in reponseObject[@"attraction_trip_tags"]) {
            //每个section中cell的个数
            NSMutableArray * rowArr = [[NSMutableArray alloc] init];
            //图片单独的数组
            NSMutableArray * rowImageUrlArr = [[NSMutableArray alloc] init];
            //第二层遍历
            for (NSDictionary * dict2 in dict[@"attraction_contents"]) {
                XYJLXResultModel * model = [[XYJLXResultModel alloc] initWithDict:dict2];
                //每个cell上的图片url的数组
                NSMutableArray * imageArr = [[NSMutableArray alloc] init];
                //第三层遍历
                for (NSDictionary * dict3 in dict2[@"notes"]) {
                    XYJLXResultModel * model = [[XYJLXResultModel alloc] initWithOtherDict:dict3];
                    if (![model.note_photo_url isEqualToString:@""]) {
                        [imageArr addObject:model.note_photo_url];
                    }
                }
                //给model中的数组赋值
                model.imageUrlArray = imageArr;
                XYJLXResultFrameModel * frameModel = [[XYJLXResultFrameModel alloc] init];
                frameModel.model = model;
                //给每个cell添加model
                [rowArr addObject:frameModel];
                //给每个imageArr添加image
                [rowImageUrlArr addObject:imageArr];
            }
            [_dataSectionArr addObject:rowArr];
            [_dataImageUrlSectionArr addObject:rowImageUrlArr];
        }
        [_tableView reloadData];
        [cellCollectionView reloadData];
    } failure:^(NSError *error) {
        
    }];
}


- (void)layoutCollectionViewWith:(CGRect)cellCollectionViewFrame andIndexRow:(NSIndexPath *)indexPath{
    collectionViewSection = indexPath;
    //初始化layout
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    //滑动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //设置每个cell的大小
    layout.itemSize = CGSizeMake(120, 100);
    //每个cell之间的距离
    layout.minimumLineSpacing = 5.0f;
    cellCollectionView = [[UICollectionView alloc] initWithFrame:cellCollectionViewFrame collectionViewLayout:layout];
    cellCollectionView.delegate = self;
    cellCollectionView.dataSource = self;
    cellCollectionView.showsVerticalScrollIndicator = NO;
    cellCollectionView.showsHorizontalScrollIndicator = NO;
    //注册cell
    [cellCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    cellCollectionView.backgroundColor = RGB(230, 241, 243);
}


#pragma mark - CollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    NSInteger count = [_dataImageUrlSectionArr[collectionViewSection.section][collectionViewSection.row] count];
    return count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    //网络加载
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:RECT_MAKE(0, 0, 120, 100)];
    [imageView sd_setImageWithURL:_dataImageUrlSectionArr[indexPathSection.section][indexPathSection.row][indexPath.row] placeholderImage:[UIImage imageNamed:@"zhanWeiTu"]];
    [cell addSubview:imageView];
    return cell;
}
@end
